function getXhr(){
    let xhr=null;
    if(window.XMLHttpRequest){
        xhr = new XMLHttpRequest();
    } else {
        //为了兼容IE6
        xhr = new ActiveXObject('Microsoft.XMLHTTP');
    }
    return xhr;
}

function Ajax(obj){

    let xhr = getXhr();

    let type = obj.type ? obj.type.toUpperCase() : 'POST';

    let url = obj.url ? obj.url : '';

    let headers = obj.headers ? obj.headers : {contentType:'application/json'};

    let resolve = obj.resolve ? obj.resolve : function(){};

    let reject = obj.reject ? obj.reject : function(){};

    let data = obj.data ? obj.data : '';

    let responseType = obj.responseType ? obj.responseType : '';
    
    let reg = new RegExp('\\?',"g");

    if(reg.test(url)){
        reject("url地址传递错误");
        return ;
    }

    let random = Math.random();

    url += "?"+random;

    if(type == 'GET'){

        reject("不支持GET提交方式");

    } else if(type == 'POST'){

        if(responseType){

            xhr.responseType = responseType;

        }
        
        xhr.open('POST', url, true);
        
        if(typeof headers == 'object'){

            for(let key in headers){
                // 如果需要像 html 表单那样 POST 数据，请使用 setRequestHeader() 来添加 http 头。
                xhr.setRequestHeader(key, headers[key]);
            }

        }else{

            reject("请求头设置有误");
            return ;

        }

        xhr.withCredentials = false;

        xhr.send(data);

    }
 
    // 处理返回数据
    xhr.onreadystatechange = function(){

        if(xhr.readyState == 4){

            if(xhr.status == 200){

                resolve(xhr.response);
                return xhr.response;
                // let resp = JSON.parse(xhr.responseText);


            } else {

                reject("提交失败，请重试");
                return false;

            }

        }

    }
    
}
